#include "engine.h"

namespace SkyID
{

//- Engine --------------------------------------------------------------------

namespace Engine
{

static RenderCallback  gRenderCallback  = NULL;
static AnimateCallback gAnimateCallback = NULL;

bool initialize(const Config &config)
{
	Log::create();
	LOG_MSG("Engine: Log created");

	if (!Audio::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize Audio");
		return false;
	}

	if (!Window::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize Window");
		return false;
	}

	if (!Render::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize Render");
		return false;
	}

	if (!VFS::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize VFS");
		return false;
	}

	if (!ResourceManager::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize ResourceManager");
		return false;
	}

	if (!ScriptEngine::initialize(config))
	{
		LOG_MSG("Engine: Cannot initialize ScriptEngine");
		return false;
	}

	LOG_MSG("Engine: initialized");
	return true;
}

void shutdown()
{
	ScriptEngine::shutdown();
	ResourceManager::shutdown();
	VFS::shutdown();
	Render::shutdown();
	Window::shutdown();
	Audio::shutdown();
	LOG_MSG("Engine: shutdown");
}

bool process()
{
	uint32 beginFrame = 0;
	while (Window::processEvents())
	{
		beginFrame = Window::getTicks();
		Render::beginScene();
		CALLBACK(gRenderCallback);
		Render::endScene();
		CALLBACK(gAnimateCallback, (Window::getTicks() - beginFrame) / 1000.0f);
	}
	return true;
}

void quit()
{
	Window::quit();
}

void setRenderCallback(RenderCallback callback)
{
	gRenderCallback = callback;
}

void setAnimateCallback(AnimateCallback callback)
{
	gAnimateCallback = callback;
}

} // namespace Engine

} // namespace SkyID
